package com.world.rickcloudendpoint.common.aspect;

import com.world.rickcloudendpoint.common.annotation.UserActionLogger;
import com.world.rickcloudendpoint.models.mongo.OptLog;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @description: 用户操作日志切面
 * @author: jlsong
 * @date: 2022/04/06 17:14:59
 * @version: 1.0
 */
@Aspect
@Component
@Log4j2
public class UserOptLogAspect {

    final MongoTemplate mongoTemplate;

    public UserOptLogAspect(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    // 配置切入点
    @Pointcut("@annotation(com.world.rickcloudendpoint.common.annotation.UserActionLogger)")
    public void logPointCut(){};


    /**
     * 处理完请求后执行
     * @param joinPoint
     * @param jsonResult
     */
    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult){
        handleLog(joinPoint, null, jsonResult);
    }

    /**
     * 拦截异常操作
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(pointcut = "logPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e){
        handleLog(joinPoint, e, null);
    }


    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult){
        try{
            // 获得注解
            UserActionLogger optLogger = getAnnotationLog(joinPoint);
            if (optLogger == null){
                return;
            }

            OptLog optLog = new OptLog();
            optLog.setTime(System.currentTimeMillis());
            if (e != null) {
                optLog.setUsername("1");
                optLog.setDesc(StringUtils.substring(e.getMessage(), 0, 2000));
            }
            // i设置方法名称
            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            // 处理u注解上的参数
            getMethodDesc(joinPoint, optLogger, optLog);
            mongoTemplate.insert(optLog);
        }catch (Exception exp){
            log.error("==前置通知异常==");
            log.error("日志异常信息 {}", exp);
        }
    }

    public void getMethodDesc(JoinPoint joinPoint, UserActionLogger userActionLogger, OptLog optLog){
        optLog.setBusinessType(userActionLogger.businessType());
        optLog.setTitle(userActionLogger.title());;
    }


    private UserActionLogger getAnnotationLog(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null){
            return method.getAnnotation(UserActionLogger.class);
        }
        return null;
    }
}
